package com.example.controller;

import ch.qos.logback.core.hook.ShutdownHook;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.entity.ShoppingCart;
import com.example.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>>  list(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart:{}",shoppingCart);
        //设置用户id，指定当前是那个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        //先通过userid的条件来查询
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //如果是菜品id,则通过菜品的信息条件查询
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            //否则通过套餐的信息条件
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //然后通过条件查询响应的单个数据
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lqw);
        if (shoppingCartOne!=null){
            //如果已经存在，就在原来的数量基础上加一
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number+1);
            shoppingCartService.updateById(shoppingCartOne);
        }else {
            //如果不存在，则添加到购物车，数量默认为一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartOne=shoppingCart;
        }

        return R.success(shoppingCartOne);
    }
    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        //根据用户id进行删除
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);

        return R.success("清除成功");

    }
    //减少购物车的量
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("查询到菜品或套餐id为：{}",shoppingCart);
        //根据用户id条件查询
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        if (dishId !=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
            //查询该菜品的信息
            ShoppingCart shoppingCartDishOne = shoppingCartService.getOne(lqw);
            Integer number = shoppingCartDishOne.getNumber();
            shoppingCartDishOne.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartDishOne);
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            //查询该套餐的信息
            ShoppingCart shoppingCartSetmealOne = shoppingCartService.getOne(lqw);
            Integer number = shoppingCartSetmealOne.getNumber();
            shoppingCartSetmealOne.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartSetmealOne);
        }
        //查询菜单和套餐的数量，如果为0则直接删除该条数据
        ShoppingCart shoppingCartDishOne2 = shoppingCartService.getOne(lqw);
        ShoppingCart shoppingCartSetmealOne2 = shoppingCartService.getOne(lqw);
        if (shoppingCartDishOne2.getNumber()==0){
            shoppingCartService.removeById(shoppingCartDishOne2);
        }
        if (shoppingCartSetmealOne2.getNumber()==0){
            shoppingCartService.removeById(shoppingCartSetmealOne2);
        }
        return R.success("减单成功");
    }
}
